//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.baidu.lbsapi.auth.tracesdk;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.json.JSONException;
import org.json.JSONObject;

public class LBSAuthManager {
    public static final int CODE_UNAUTHENTICATE = 601;
    public static final int CODE_AUTHENTICATING = 602;
    public static final int CODE_AUTHENTICATE_SUCC = 0;
    public static final int CODE_INNER_ERROR = -1;
    public static final int CODE_NETWORK_INVALID = -10;
    public static final int CODE_NETWORK_FAILED = -11;
    public static final int CODE_KEY_NOT_EXIST = 101;
    public static final String VERSION = "1.0.21.20161104";
    private static Context a;
    private c b = null;
    private e c = null;
    private static l d = null;
    private static int e = 0;
    private static Hashtable f = new Hashtable();
    private static LBSAuthManager g;
    private final Handler h = new h(this, Looper.getMainLooper());

    private LBSAuthManager(Context var1) {
        a = var1;
        if(d != null && !d.isAlive()) {
            d = null;
        }

        com.baidu.lbsapi.auth.tracesdk.a.b("BaiduApiAuth SDK Version:1.0.21.20161104");
        this.d();
    }

    private void d() {
        Class var1 = LBSAuthManager.class;
        synchronized(LBSAuthManager.class) {
            if(d == null) {
                (d = new l("auth")).start();

                while(d.a == null) {
                    try {
                        if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                            com.baidu.lbsapi.auth.tracesdk.a.a("wait for create auth thread.");
                        }

                        Thread.sleep(3L);
                    } catch (InterruptedException var3) {
                        var3.printStackTrace();
                    }
                }
            }

        }
    }

    public static LBSAuthManager getInstance(Context var0) {
        if(g == null) {
            Class var1 = LBSAuthManager.class;
            synchronized(LBSAuthManager.class) {
                if(g == null) {
                    g = new LBSAuthManager(var0);
                }
            }
        } else if(var0 == null) {
            if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                com.baidu.lbsapi.auth.tracesdk.a.c("input context is null");
                (new RuntimeException("here")).printStackTrace();
            }
        } else {
            a = var0;
        }

        return g;
    }

    public String getKey() {
        if(a == null) {
            return "";
        } else {
            try {
                return this.getPublicKey(a);
            } catch (NameNotFoundException var2) {
                var2.printStackTrace();
                return "";
            }
        }
    }

    public String getMCode() {
        return a == null?"":com.baidu.lbsapi.auth.tracesdk.b.a(a);
    }

    @SuppressLint("WrongConstant")
    public String getPublicKey(Context var1) throws NameNotFoundException {
        String var2 = var1.getPackageName();
        return var1.getPackageManager().getApplicationInfo(var2, 128).metaData.getString("com.baidu.lbsapi.API_KEY");
    }

    public String getCUID() {
        if(a == null) {
            return "";
        } else {
            String var1 = "";

            try {
                var1 = com.baidu.a.a.a.a.b.a.a(a);
            } catch (Exception var3) {
                if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                    var3.printStackTrace();
                }
            }

            return var1;
        }
    }

    public String getDeviceID() {
        if(a == null) {
            return "";
        } else {
            String var1 = "";

            try {
                var1 = com.baidu.a.a.a.a.b.a.b(a);
            } catch (Exception var3) {
                if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                    var3.printStackTrace();
                }
            }

            return var1;
        }
    }

    public String getIMEI() {
        if(a == null) {
            return "";
        } else {
            String var1 = "";

            try {
                var1 = com.baidu.a.a.a.a.b.a.c(a);
            } catch (Exception var3) {
                if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                    var3.printStackTrace();
                }
            }

            return var1;
        }
    }

    @SuppressLint("WrongConstant")
    private String a(Context var1, String var2) {
        String var3 = "";
        String var4 = var1.getPackageName();

        LBSAuthManagerListener var6;
        try {
            ApplicationInfo var7;
            if((var7 = var1.getPackageManager().getApplicationInfo(var4, 128)).metaData == null) {
                if((var6 = (LBSAuthManagerListener)f.get(var2)) != null) {
                    var6.onAuthResult(101, ErrorMessage.a(101, "AndroidManifest.xml的application中没有meta-data标签"));
                }
            } else if(((var3 = var7.metaData.getString("com.baidu.lbsapi.API_KEY")) == null || var3.equals("")) && (var6 = (LBSAuthManagerListener)f.get(var2)) != null) {
                var6.onAuthResult(101, ErrorMessage.a(101, "无法在AndroidManifest.xml中获取com.baidu.android.lbs.API_KEY的值"));
            }
        } catch (NameNotFoundException var5) {
            if((var6 = (LBSAuthManagerListener)f.get(var2)) != null) {
                var6.onAuthResult(101, ErrorMessage.a(101, "无法在AndroidManifest.xml中获取com.baidu.android.lbs.API_KEY的值"));
            }
        }

        return var3;
    }

    private synchronized void a(String var1, String var2) {
        if(var1 == null) {
            var1 = this.e();
        }

        Message var3 = this.h.obtainMessage();
        byte var4 = -1;

        Bundle var6;
        try {
            JSONObject var7;
            if(!(var7 = new JSONObject(var1)).has("status")) {
                var7.put("status", -1);
            }

            if(!var7.has("current")) {
                var7.put("current", System.currentTimeMillis());
            }

            this.c(var7.toString());
            if(var7.has("current")) {
                var7.remove("current");
            }

            int var8 = var7.getInt("status");
            var3.what = var8;
            var3.obj = var7.toString();
            (var6 = new Bundle()).putString("listenerKey", var2);
            var3.setData(var6);
            this.h.sendMessage(var3);
        } catch (JSONException var5) {
            var5.printStackTrace();
            var3.what = var4;
            var3.obj = new JSONObject();
            (var6 = new Bundle()).putString("listenerKey", var2);
            var3.setData(var6);
            this.h.sendMessage(var3);
        }

        d.c();
        --e;
        if(com.baidu.lbsapi.auth.tracesdk.a.a) {
            com.baidu.lbsapi.auth.tracesdk.a.a("httpRequest called mAuthCounter-- = " + e);
        }

        if(e == 0 && d != null) {
            d.a();
            d = null;
        }

    }

    private int a(String var1) {
        int var2 = -1;

        try {
            JSONObject var11;
            if(!(var11 = new JSONObject(var1)).has("status")) {
                var11.put("status", -1);
            }

            var2 = var11.getInt("status");
            long var4;
            if(var11.has("current") && var2 == 0) {
                var4 = var11.getLong("current");
                long var6;
                if((double)((var6 = System.currentTimeMillis()) - var4) / 3600000.0D >= 24.0D) {
                    var2 = 601;
                } else {
                    SimpleDateFormat var3;
                    String var13 = (var3 = new SimpleDateFormat("yyyy-MM-dd")).format(Long.valueOf(var6));
                    String var12 = var3.format(Long.valueOf(var4));
                    if(!var13.equals(var12)) {
                        var2 = 601;
                    }
                }
            }

            if(var11.has("current") && var2 == 602) {
                var4 = var11.getLong("current");
                if((double)((System.currentTimeMillis() - var4) / 1000L) > 180.0D) {
                    var2 = 601;
                }
            }
        } catch (JSONException var10) {
            var10.printStackTrace();
        }

        return var2;
    }

    public int authenticate(boolean var1, String var2, Hashtable var3, LBSAuthManagerListener var4) {
        Class var5 = LBSAuthManager.class;
        synchronized(LBSAuthManager.class) {
            String var6 = "" + System.currentTimeMillis();
            if(var4 != null) {
                f.put(var6, var4);
            }

            String var10;
            if((var10 = this.a(a, var6)) != null && !var10.equals("")) {
                ++e;
                if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                    com.baidu.lbsapi.auth.tracesdk.a.a(" mAuthCounter  ++ = " + e);
                }

                var10 = this.e();
                if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                    com.baidu.lbsapi.auth.tracesdk.a.a("getAuthMessage from cache:" + var10);
                }

                int var11;
                if((var11 = this.a(var10)) == 601) {
                    try {
                        this.c((new JSONObject()).put("status", 602).toString());
                    } catch (JSONException var8) {
                        var8.printStackTrace();
                    }
                }

                this.d();
                if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                    com.baidu.lbsapi.auth.tracesdk.a.a("mThreadLooper.mHandler = " + d.a);
                }

                if(d != null && null != d.a) {
                    d.a.post(new i(this, var11, var1, var6, var2, var3));
                    return var11;
                } else {
                    return -1;
                }
            } else {
                return 101;
            }
        }
    }

    private boolean b(String var1) {
        var1 = this.a(a, var1);
        String var2 = this.e();
        String var3 = "";

        try {
            JSONObject var5;
            if(!(var5 = new JSONObject(var2)).has("ak")) {
                return true;
            }

            var3 = var5.getString("ak");
        } catch (JSONException var4) {
            var4.printStackTrace();
        }

        return var1 != null && var3 != null && !var1.equals(var3);
    }

    private void a(boolean var1, String var2, Hashtable var3, String var4) {
        String var5;
        if((var5 = this.a(a, var4)) != null && !var5.equals("")) {
            HashMap var6;
            (var6 = new HashMap()).put("url", "https://api.map.baidu.com/sdkcs/verify");
            com.baidu.lbsapi.auth.tracesdk.a.a("url:https://api.map.baidu.com/sdkcs/verify");
            var6.put("output", "json");
            var6.put("ak", var5);
            com.baidu.lbsapi.auth.tracesdk.a.a("ak:" + var5);
            var6.put("mcode", com.baidu.lbsapi.auth.tracesdk.b.a(a));
            var6.put("from", "lbs_yunsdk");
            String var7;
            if(var3 != null && var3.size() > 0) {
                Iterator var11 = var3.entrySet().iterator();

                while(var11.hasNext()) {
                    Entry var13;
                    var7 = (String)(var13 = (Entry)var11.next()).getKey();
                    var5 = (String)var13.getValue();
                    if(!TextUtils.isEmpty(var7) && !TextUtils.isEmpty(var5)) {
                        var6.put(var7, var5);
                    }
                }
            }

            String var12 = "";

            try {
                var12 = com.baidu.a.a.a.a.b.a.a(a);
            } catch (Exception var10) {
                ;
            }

            com.baidu.lbsapi.auth.tracesdk.a.a("cuid:" + var12);
            if(!TextUtils.isEmpty(var12)) {
                var6.put("cuid", var12);
            } else {
                var6.put("cuid", "");
            }

            var6.put("pcn", a.getPackageName());
            var6.put("version", "1.0.21.20161104");
            var5 = "";

            try {
                var5 = com.baidu.lbsapi.auth.tracesdk.b.c(a);
            } catch (Exception var9) {
                ;
            }

            if(!TextUtils.isEmpty(var5)) {
                var6.put("macaddr", var5);
            } else {
                var6.put("macaddr", "");
            }

            var7 = "";

            try {
                var7 = com.baidu.lbsapi.auth.tracesdk.b.a();
            } catch (Exception var8) {
                ;
            }

            if(!TextUtils.isEmpty(var7)) {
                var6.put("language", var7);
            } else {
                var6.put("language", "");
            }

            if(var1) {
                var6.put("force", var1?"1":"0");
            }

            if(var2 == null) {
                var6.put("from_service", "");
            } else {
                var6.put("from_service", var2);
            }

            this.b = new c(a);
            this.b.a(var6, new j(this, var4));
        }
    }

    private void a(boolean var1, String var2, Hashtable var3, String[] var4, String var5) {
        String var6;
        if((var6 = this.a(a, var5)) != null && !var6.equals("")) {
            HashMap var7;
            (var7 = new HashMap()).put("url", "https://api.map.baidu.com/sdkcs/verify");
            var7.put("output", "json");
            var7.put("ak", var6);
            var7.put("from", "lbs_yunsdk");
            String var8;
            if(var3 != null && var3.size() > 0) {
                Iterator var12 = var3.entrySet().iterator();

                while(var12.hasNext()) {
                    Entry var14;
                    var8 = (String)(var14 = (Entry)var12.next()).getKey();
                    var6 = (String)var14.getValue();
                    if(!TextUtils.isEmpty(var8) && !TextUtils.isEmpty(var6)) {
                        var7.put(var8, var6);
                    }
                }
            }

            String var13 = "";

            try {
                var13 = com.baidu.a.a.a.a.b.a.a(a);
            } catch (Exception var11) {
                ;
            }

            if(!TextUtils.isEmpty(var13)) {
                var7.put("cuid", var13);
            } else {
                var7.put("cuid", "");
            }

            var7.put("pcn", a.getPackageName());
            var7.put("version", "1.0.21.20161104");
            var6 = "";

            try {
                var6 = com.baidu.lbsapi.auth.tracesdk.b.c(a);
            } catch (Exception var10) {
                ;
            }

            if(!TextUtils.isEmpty(var6)) {
                var7.put("macaddr", var6);
            } else {
                var7.put("macaddr", "");
            }

            var8 = "";

            try {
                var8 = com.baidu.lbsapi.auth.tracesdk.b.a();
            } catch (Exception var9) {
                ;
            }

            if(!TextUtils.isEmpty(var8)) {
                var7.put("language", var8);
            } else {
                var7.put("language", "");
            }

            if(var1) {
                var7.put("force", var1?"1":"0");
            }

            if(var2 == null) {
                var7.put("from_service", "");
            } else {
                var7.put("from_service", var2);
            }

            this.c = new e(a);
            this.c.a(var7, var4, new k(this, var5));
        }
    }

    private String e() {
        return a.getSharedPreferences("authStatus_" + this.a(a), 0).getString("status", "{\"status\":601}");
    }

    private void c(String var1) {
        a.getSharedPreferences("authStatus_" + this.a(a), 0).edit().putString("status", var1).commit();
    }

    private String a(Context var1) {
        int var2 = Process.myPid();
        @SuppressLint("WrongConstant")
        List var5 = ((ActivityManager)var1.getSystemService("activity")).getRunningAppProcesses();
        if(var5 != null) {
            Iterator var6 = var5.iterator();

            while(var6.hasNext()) {
                RunningAppProcessInfo var3;
                if((var3 = (RunningAppProcessInfo)var6.next()).pid == var2) {
                    return var3.processName;
                }
            }
        }

        String var7 = null;

        try {
            var7 = this.a(var2);
        } catch (IOException var4) {
            ;
        }

        return var7 != null?var7:a.getPackageName();
    }

    private String a(int var1) throws IOException {
        String var2 = null;
        FileInputStream var3 = null;
        InputStreamReader var4 = null;
        BufferedReader var5 = null;

        try {
            File var12 = new File("/proc/" + var1 + "/cmdline");
            var3 = new FileInputStream(var12);
            var4 = new InputStreamReader(var3);
            var2 = (var5 = new BufferedReader(var4)).readLine();
        } catch (FileNotFoundException var9) {
            ;
        } catch (IOException var10) {
            ;
        } finally {
            if(var5 != null) {
                var5.close();
            }

            if(var4 != null) {
                var4.close();
            }

            if(var3 != null) {
                var3.close();
            }

        }

        return var2;
    }

    static Hashtable a() {
        return LBSAuthManager.f;
    }

    static boolean a(LBSAuthManager arg1, String arg2) {
        return arg1.b(arg2);
    }

    static void a(LBSAuthManager arg0, String arg1, String arg2) {
        arg0.a(arg1, arg2);
    }

    static void a(LBSAuthManager arg0, boolean arg1, String arg2, Hashtable arg3, String[] arg4, String
            arg5) {
        arg0.a(arg1, arg2, arg3, arg4, arg5);
    }

    static void a(LBSAuthManager arg0, boolean arg1, String arg2, Hashtable arg3, String arg4) {
        arg0.a(arg1, arg2, arg3, arg4);
    }

    static Context b() {
        return LBSAuthManager.a;
    }

    static l c() {
        return LBSAuthManager.d;
    }
}
